package com.ecb.config;

import com.ecb.business.dto.api.ApiResult;
import com.ecb.business.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 全局的异常处理类
 */
@ControllerAdvice(annotations = {RestController.class , Controller.class})
@ResponseBody
@Slf4j
public class GlobalRestExceptionHandler {

    @Autowired
    HttpServletRequest request;

//    @ExceptionHandler(value = ResultBaseException.class)
//    public ApiResult easiBaseSysExceptionHandler(ResultBaseException e) {
//        ApiResult restResult = new ApiResult(e.getCode() , e.getMessage() , e.getBizCode() , e.getBizMessage());
//        return restResult;
//    }

    @ExceptionHandler(value = NullPointerException.class)
    public ApiResult nullPointerExceptionHandler(Exception e) {
        e.printStackTrace();
        ApiResult restResult = new ApiResult("F","500","500","","");
        return restResult;
    }
    @ExceptionHandler(value = BusinessException.class)
    public ApiResult businessExceptionHandler(Exception e) {
        e.printStackTrace();
        ApiResult restResult = new ApiResult("F","500",e.getMessage(),"","");
        return restResult;
    }

    @ExceptionHandler(value = org.springframework.web.client.RestClientException.class)
    public ApiResult restClientExceptionHandler(Exception e) {
        ApiResult restResult = new ApiResult("F","500","500","","");
        return restResult;
    }

    @ExceptionHandler(value = Exception.class)
    public ApiResult exceptionHandler(Exception e) {
        log.error(e.getMessage() , e);
        ApiResult restResult = new ApiResult("F","500","500","","");
        return restResult;
    }


}
